package com.xu.cloud.shorturl.controller;

import com.xu.cloud.common.api.XuApiMethod;
import com.xu.cloud.common.pojo.Result;
import com.xu.cloud.common.util.HttpServletUtil;
import com.xu.cloud.shorturl.service.ShortUrlService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xuguan
 * @since 2024/3/5
 */
@RestController
@RequiredArgsConstructor
public class ShortUrlRedirectController {
    private final ShortUrlService shortUrlService;

    @GetMapping("/")
    public Result<Void> rootUrl() {
        return Result.ok(null);
    }

    @XuApiMethod(apiCode = "XUSU0003", apiDesc = "短地址重定向")
    @GetMapping("/r/{shortUrl}")
    public Result<Void> shortUrlRedirect(@PathVariable String shortUrl) {
        final String originUrl = shortUrlService.queryOriginUrlByShortUrl(shortUrl);
        HttpServletUtil.temporaryRedirect(originUrl);

        return Result.ok(null);
    }
}
